#include "precompiled.h"
#include "Player.h"
#include "Camera_Alt.h"
#include "RenderDefines.h"
#include "Gun.h"
#include "ParticleManager.h"

const glm::vec3 CAMERA_ACCELERATION( 8.0f );
const float   CAMERA_FOVX = 45.0f;
const glm::vec3 CAMERA_POS(0.0f, -50.0f, 0.0f);
const float   CAMERA_SPEED_ROTATION = 0.4f;
const float   CAMERA_SPEED_FLIGHT_YAW = 100.0f;
const glm::vec3 CAMERA_VELOCITY( 2.0f );
const float   CAMERA_ZFAR = 10000.0f;
const float   CAMERA_ZNEAR = 0.1f;

Player::Player(void)
{
	init_cam_alt();
	init_gun();
}


Player::~Player(void)
{
	delete cam_alt;
}

void Player::init_gun(){
	gun = new Gun();
	gun->Initialize( 10.0f );
}

void Player::init_cam_alt(){
	cam_alt = new Camera_Alt();
	cam_alt->perspective(CAMERA_FOVX,
		4.0f/3.0f,//static_cast<float>(RENDER_WIDTH) / static_cast<float>(RENDER_HEIGHT),
		CAMERA_ZNEAR, CAMERA_ZFAR);

	cam_alt->setPosition(CAMERA_POS);
	cam_alt->setAcceleration(CAMERA_ACCELERATION);
	cam_alt->setVelocity(CAMERA_VELOCITY);
}
void Player::update_cam_alt( GLdouble time ){
	float heading = 0.0f;
	float pitch = 0.0f;
	float roll = 0.0f;
	glm::vec3 direction;

	//getMovementDirection(direction);


	glm::vec2 mouse_dist_from_window_center = cam_alt->get_mouseScreenDistanceFromCenter();
	pitch = mouse_dist_from_window_center.y * CAMERA_SPEED_ROTATION;
	heading = mouse_dist_from_window_center.x * CAMERA_SPEED_ROTATION;

	cam_alt->rotate(heading, pitch, 0.0f);

	//cam_alt->updatePosition(direction, time);
}

void Player::getMovementDirection(glm::vec3 &direction){}

glm::vec3 Player::getMovementDirection(){
	return cam_alt->getCurrentVelocity();
}

// Get our extents with our current transformations.
glm::vec3 Player::Get_Extents( ){
	// Give our player an extent of a 10x10 block.
	//glm::vec3 extents(5.0f);

	//extents += cam_alt->getPosition();

	return glm::vec3(5.0f);
}
glm::vec3 Player::GetPosition(){
	return glm::vec3 ( cam_alt->getPosition() );
}

void Player::Update( GLdouble time ){
	update_cam_alt( time * 100.0f );

	gun->TransformToView( cam_alt->getViewMatrix(), 
		glm::vec4(cam_alt->getPosition(),1.0f),
		glm::vec3(cam_alt->get_mouseScreenDistanceFromCenter(),1.0f),
		cam_alt->getViewDirection(),
		cam_alt->getEuler() );


	if( glfwGetMouseButton( GLFW_MOUSE_BUTTON_LEFT) ){
		ParticleManager::RunParticles( "dbg" );
	}
}
void Player::Draw(){
	gun->Submit_Ticket( cam_alt );
}

glm::mat4* Player::getCameraMatrix(  ){
	return cam_alt->getCameraMatrix();
}


glm::vec3 Player::getDirection(){
	return cam_alt->getViewDirection();
}

void Player::setPosition( float x, float y, float z){
	cam_alt->setPosition( x, y, z );
}

glm::vec3 const Player::get_gunAimFrom() const{
	return cam_alt->getPosition() + gun->get_AimFrom();
}
glm::vec3 const Player::get_gunAimTo() const{
	return gun->get_AimTo();
}
glm::vec3 const Player::get_gunButt() const{
	return cam_alt->getPosition() + gun->get_butt();
}
glm::vec3 const Player::get_gunHead() const{
	return cam_alt->getPosition() + gun->get_head();
}

glm::vec3 const Player::get_lookFrom(){
	return cam_alt->getPosition();
}
glm::vec3 const Player::get_lookTo(){
	return cam_alt->getPosition() + (cam_alt->getViewDirection() * CAMERA_ZFAR);
}